{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pima Indians Diabetes Data Set特征工程\n",
    "数据说明：Pima Indians Diabetes Data Set（皮马印第安人糖尿病数据集） 根据现有的医疗信息预测5年内皮马印第安人糖尿病发作的概率。   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.import 工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.读取文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data_path = 'D:\\DeepLearning_Dataset\\PIMA-indians/pima-indians-diabetes .csv'\n",
    "train = pd.read_csv(data_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在数据探索中我们看到很多列的最小值为0。而在一些特定列代表的变量中，0值并没有意义，这就表名该值无效或为缺失值。<br/>\n",
    "\n",
    "比如下列变量的最小值为0时数据无意义：<br/>\n",
    "1、血浆葡萄糖浓度<br/>\n",
    "2、舒张压<br/>\n",
    "3、肱三头肌皮褶厚度<br/>\n",
    "4、餐后血清胰岛素<br/>\n",
    "5、体重指数<br/>\n",
    "\n",
    "在Pandas的DataFrame中，通过replace()函数可以很方便的将我们感兴趣的数据子集的值标记为NaN。<br/>\n",
    "标记完缺失值之后，可以利用isnull()函数将数据集中所有的NaN值标记为True，然后就可以得到每一列中缺失值的数量了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pregnancies                   0\n",
      "Glucose                       5\n",
      "BloodPressure                35\n",
      "SkinThickness               227\n",
      "Insulin                     374\n",
      "BMI                          11\n",
      "DiabetesPedigreeFunction      0\n",
      "Age                           0\n",
      "Outcome                       0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "NaN_col_names = ['Glucose','BloodPressure','SkinThickness','Insulin','BMI']\n",
    "train[NaN_col_names] = train[NaN_col_names].replace(0, np.NaN)\n",
    "print(train.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用中值填补为0的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pregnancies                 0\n",
      "Glucose                     0\n",
      "BloodPressure               0\n",
      "SkinThickness               0\n",
      "Insulin                     0\n",
      "BMI                         0\n",
      "DiabetesPedigreeFunction    0\n",
      "Age                         0\n",
      "Outcome                     0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "medians = train.median() \n",
    "train = train.fillna(medians)\n",
    "# 也可使用下列语句增加一个变量\n",
    "# train['Insulin_Missing'] = train['Insulin'].apply(lambda x: 1 if pd.isnull(x) else 0)\n",
    "print(train.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 原始数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据集分开\n",
    "y_train = train['Outcome']   \n",
    "X_train = train.drop(['Outcome'], axis=1)\n",
    "\n",
    "# 保存特征名字\n",
    "columns_org = X_train.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### feat编码：log(x+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies_log</th>\n",
       "      <th>Glucose_log</th>\n",
       "      <th>BloodPressure_log</th>\n",
       "      <th>SkinThickness_log</th>\n",
       "      <th>Insulin_log</th>\n",
       "      <th>BMI_log</th>\n",
       "      <th>DiabetesPedigreeFunction_log</th>\n",
       "      <th>Age_log</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.945910</td>\n",
       "      <td>5.003946</td>\n",
       "      <td>4.290459</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>4.836282</td>\n",
       "      <td>3.543854</td>\n",
       "      <td>0.486738</td>\n",
       "      <td>3.931826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.454347</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.401197</td>\n",
       "      <td>4.836282</td>\n",
       "      <td>3.317816</td>\n",
       "      <td>0.300845</td>\n",
       "      <td>3.465736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.197225</td>\n",
       "      <td>5.214936</td>\n",
       "      <td>4.174387</td>\n",
       "      <td>3.401197</td>\n",
       "      <td>4.836282</td>\n",
       "      <td>3.190476</td>\n",
       "      <td>0.514021</td>\n",
       "      <td>3.496508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.693147</td>\n",
       "      <td>4.499810</td>\n",
       "      <td>4.204693</td>\n",
       "      <td>3.178054</td>\n",
       "      <td>4.553877</td>\n",
       "      <td>3.370738</td>\n",
       "      <td>0.154436</td>\n",
       "      <td>3.091042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.927254</td>\n",
       "      <td>3.713572</td>\n",
       "      <td>3.583519</td>\n",
       "      <td>5.129899</td>\n",
       "      <td>3.786460</td>\n",
       "      <td>1.190279</td>\n",
       "      <td>3.526361</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies_log  Glucose_log  BloodPressure_log  SkinThickness_log  \\\n",
       "0         1.945910     5.003946           4.290459           3.583519   \n",
       "1         0.693147     4.454347           4.204693           3.401197   \n",
       "2         2.197225     5.214936           4.174387           3.401197   \n",
       "3         0.693147     4.499810           4.204693           3.178054   \n",
       "4         0.000000     4.927254           3.713572           3.583519   \n",
       "\n",
       "   Insulin_log   BMI_log  DiabetesPedigreeFunction_log   Age_log  \n",
       "0     4.836282  3.543854                      0.486738  3.931826  \n",
       "1     4.836282  3.317816                      0.300845  3.465736  \n",
       "2     4.836282  3.190476                      0.514021  3.496508  \n",
       "3     4.553877  3.370738                      0.154436  3.091042  \n",
       "4     5.129899  3.786460                      1.190279  3.526361  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_log = np.log1p(X_train)\n",
    "\n",
    "# 重新组成DataFrame\n",
    "feat_names1 = columns_org + \"_log\"\n",
    "X_train_log = pd.DataFrame(columns = feat_names1, data = X_train_log.values)\n",
    "\n",
    "X_train_log.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### feat编码：TF-IDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies_tfidf</th>\n",
       "      <th>Glucose_tfidf</th>\n",
       "      <th>BloodPressure_tfidf</th>\n",
       "      <th>SkinThickness_tfidf</th>\n",
       "      <th>Insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>DiabetesPedigreeFunction_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.031783</td>\n",
       "      <td>0.678247</td>\n",
       "      <td>0.329958</td>\n",
       "      <td>0.160396</td>\n",
       "      <td>0.572844</td>\n",
       "      <td>0.153980</td>\n",
       "      <td>0.002873</td>\n",
       "      <td>0.229137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.006705</td>\n",
       "      <td>0.493079</td>\n",
       "      <td>0.382861</td>\n",
       "      <td>0.168227</td>\n",
       "      <td>0.725116</td>\n",
       "      <td>0.154305</td>\n",
       "      <td>0.002036</td>\n",
       "      <td>0.179829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.039180</td>\n",
       "      <td>0.775369</td>\n",
       "      <td>0.271167</td>\n",
       "      <td>0.122873</td>\n",
       "      <td>0.529624</td>\n",
       "      <td>0.098722</td>\n",
       "      <td>0.002847</td>\n",
       "      <td>0.135584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.007643</td>\n",
       "      <td>0.588463</td>\n",
       "      <td>0.436388</td>\n",
       "      <td>0.152075</td>\n",
       "      <td>0.621523</td>\n",
       "      <td>0.185796</td>\n",
       "      <td>0.001104</td>\n",
       "      <td>0.138851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.596386</td>\n",
       "      <td>0.174127</td>\n",
       "      <td>0.152361</td>\n",
       "      <td>0.731335</td>\n",
       "      <td>0.187622</td>\n",
       "      <td>0.009960</td>\n",
       "      <td>0.143655</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies_tfidf  Glucose_tfidf  BloodPressure_tfidf  SkinThickness_tfidf  \\\n",
       "0           0.031783       0.678247             0.329958             0.160396   \n",
       "1           0.006705       0.493079             0.382861             0.168227   \n",
       "2           0.039180       0.775369             0.271167             0.122873   \n",
       "3           0.007643       0.588463             0.436388             0.152075   \n",
       "4           0.000000       0.596386             0.174127             0.152361   \n",
       "\n",
       "   Insulin_tfidf  BMI_tfidf  DiabetesPedigreeFunction_tfidf  Age_tfidf  \n",
       "0       0.572844   0.153980                        0.002873   0.229137  \n",
       "1       0.725116   0.154305                        0.002036   0.179829  \n",
       "2       0.529624   0.098722                        0.002847   0.135584  \n",
       "3       0.621523   0.185796                        0.001104   0.138851  \n",
       "4       0.731335   0.187622                        0.009960   0.143655  "
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfTransformer\n",
    "tfidf = TfidfTransformer()\n",
    "\n",
    "# 输出稀疏矩阵\n",
    "X_train_tfidf = tfidf.fit_transform(X_train).toarray()\n",
    "\n",
    "# 重新组成DataFrame,为了可视化\n",
    "feat_names2 = columns_org + \"_tfidf\"\n",
    "X_train_tfidf = pd.DataFrame(columns = feat_names2, data = X_train_tfidf)\n",
    "\n",
    "X_train_tfidf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 原始数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对原始数据缩放\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 构造输入特征的标准化器\n",
    "ms_org = StandardScaler()\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_org = X_train.columns\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train = ms_org.fit_transform(X_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### log数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对log数据缩放\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 构造输入特征的标准化器\n",
    "ms_log = StandardScaler()\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_log = X_train_log.columns\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train_log = ms_log.fit_transform(X_train_log)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TF-IDF数据标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对tf-idf数据缩放\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 构造输入特征的标准化器\n",
    "ms_tfidf = StandardScaler()\n",
    "\n",
    "#保存特征名字，用于结果保存为csv\n",
    "feat_names_tfidf = X_train_tfidf.columns\n",
    "\n",
    "# 用训练训练模型（得到均值和标准差）：fit\n",
    "# 并对训练数据进行特征缩放：transform\n",
    "X_train_tfidf = ms_tfidf.fit_transform(X_train_tfidf)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.特征处理结果存为新文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将原数据存为csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.639947</td>\n",
       "      <td>0.866045</td>\n",
       "      <td>-0.031990</td>\n",
       "      <td>0.670643</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>0.166619</td>\n",
       "      <td>0.468492</td>\n",
       "      <td>1.425995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.844885</td>\n",
       "      <td>-1.205066</td>\n",
       "      <td>-0.528319</td>\n",
       "      <td>-0.012301</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>-0.852200</td>\n",
       "      <td>-0.365061</td>\n",
       "      <td>-0.190672</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.233880</td>\n",
       "      <td>2.016662</td>\n",
       "      <td>-0.693761</td>\n",
       "      <td>-0.012301</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>-1.332500</td>\n",
       "      <td>0.604397</td>\n",
       "      <td>-0.105584</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.844885</td>\n",
       "      <td>-1.073567</td>\n",
       "      <td>-0.528319</td>\n",
       "      <td>-0.695245</td>\n",
       "      <td>-0.540642</td>\n",
       "      <td>-0.633881</td>\n",
       "      <td>-0.920763</td>\n",
       "      <td>-1.041549</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.141852</td>\n",
       "      <td>0.504422</td>\n",
       "      <td>-2.679076</td>\n",
       "      <td>0.670643</td>\n",
       "      <td>0.316566</td>\n",
       "      <td>1.549303</td>\n",
       "      <td>5.484909</td>\n",
       "      <td>-0.020496</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies   Glucose  BloodPressure  SkinThickness   Insulin       BMI  \\\n",
       "0     0.639947  0.866045      -0.031990       0.670643 -0.181541  0.166619   \n",
       "1    -0.844885 -1.205066      -0.528319      -0.012301 -0.181541 -0.852200   \n",
       "2     1.233880  2.016662      -0.693761      -0.012301 -0.181541 -1.332500   \n",
       "3    -0.844885 -1.073567      -0.528319      -0.695245 -0.540642 -0.633881   \n",
       "4    -1.141852  0.504422      -2.679076       0.670643  0.316566  1.549303   \n",
       "\n",
       "   DiabetesPedigreeFunction       Age  Outcome  \n",
       "0                  0.468492  1.425995        1  \n",
       "1                 -0.365061 -0.190672        0  \n",
       "2                  0.604397 -0.105584        1  \n",
       "3                 -0.920763 -1.041549        0  \n",
       "4                  5.484909 -0.020496        1  "
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存为csv格式\n",
    "X_train = pd.DataFrame(columns = feat_names_org, data = X_train)\n",
    "train = pd.concat([X_train, y_train], axis = 1)\n",
    "train.to_csv('FE_pima-indians-diabetes_org.csv',index = False,header=True)\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将log数据存为csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies_log</th>\n",
       "      <th>Glucose_log</th>\n",
       "      <th>BloodPressure_log</th>\n",
       "      <th>SkinThickness_log</th>\n",
       "      <th>Insulin_log</th>\n",
       "      <th>BMI_log</th>\n",
       "      <th>DiabetesPedigreeFunction_log</th>\n",
       "      <th>Age_log</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.825781</td>\n",
       "      <td>0.909473</td>\n",
       "      <td>0.051923</td>\n",
       "      <td>0.715064</td>\n",
       "      <td>0.018491</td>\n",
       "      <td>0.266621</td>\n",
       "      <td>0.612059</td>\n",
       "      <td>1.437767</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.802604</td>\n",
       "      <td>-1.312334</td>\n",
       "      <td>-0.446834</td>\n",
       "      <td>0.133506</td>\n",
       "      <td>0.018491</td>\n",
       "      <td>-0.840901</td>\n",
       "      <td>-0.324994</td>\n",
       "      <td>-0.050575</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.152449</td>\n",
       "      <td>1.762418</td>\n",
       "      <td>-0.623068</td>\n",
       "      <td>0.133506</td>\n",
       "      <td>0.018491</td>\n",
       "      <td>-1.464829</td>\n",
       "      <td>0.749586</td>\n",
       "      <td>0.047687</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.802604</td>\n",
       "      <td>-1.128548</td>\n",
       "      <td>-0.446834</td>\n",
       "      <td>-0.578264</td>\n",
       "      <td>-0.552520</td>\n",
       "      <td>-0.581597</td>\n",
       "      <td>-1.063014</td>\n",
       "      <td>-1.247065</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.703581</td>\n",
       "      <td>0.599436</td>\n",
       "      <td>-3.302832</td>\n",
       "      <td>0.715064</td>\n",
       "      <td>0.612171</td>\n",
       "      <td>1.455322</td>\n",
       "      <td>4.158488</td>\n",
       "      <td>0.143015</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies_log  Glucose_log  BloodPressure_log  SkinThickness_log  \\\n",
       "0         0.825781     0.909473           0.051923           0.715064   \n",
       "1        -0.802604    -1.312334          -0.446834           0.133506   \n",
       "2         1.152449     1.762418          -0.623068           0.133506   \n",
       "3        -0.802604    -1.128548          -0.446834          -0.578264   \n",
       "4        -1.703581     0.599436          -3.302832           0.715064   \n",
       "\n",
       "   Insulin_log   BMI_log  DiabetesPedigreeFunction_log   Age_log  Outcome  \n",
       "0     0.018491  0.266621                      0.612059  1.437767        1  \n",
       "1     0.018491 -0.840901                     -0.324994 -0.050575        0  \n",
       "2     0.018491 -1.464829                      0.749586  0.047687        1  \n",
       "3    -0.552520 -0.581597                     -1.063014 -1.247065        0  \n",
       "4     0.612171  1.455322                      4.158488  0.143015        1  "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存为csv格式\n",
    "X_train_log = pd.DataFrame(columns = feat_names_log, data = X_train_log)\n",
    "train_log = pd.concat([X_train_log, y_train], axis = 1)\n",
    "train_log.to_csv('FE_pima-indians-diabetes_log.csv',index = False,header=True)\n",
    "train_log.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将TF-IDF数据存为csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies_tfidf</th>\n",
       "      <th>Glucose_tfidf</th>\n",
       "      <th>BloodPressure_tfidf</th>\n",
       "      <th>SkinThickness_tfidf</th>\n",
       "      <th>Insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>DiabetesPedigreeFunction_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.521271</td>\n",
       "      <td>0.800996</td>\n",
       "      <td>-0.367192</td>\n",
       "      <td>0.308084</td>\n",
       "      <td>-0.386666</td>\n",
       "      <td>-0.187761</td>\n",
       "      <td>0.315661</td>\n",
       "      <td>1.107942</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.789470</td>\n",
       "      <td>-0.895213</td>\n",
       "      <td>0.201283</td>\n",
       "      <td>0.465320</td>\n",
       "      <td>0.684506</td>\n",
       "      <td>-0.180681</td>\n",
       "      <td>-0.186748</td>\n",
       "      <td>0.259045</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.907893</td>\n",
       "      <td>1.690672</td>\n",
       "      <td>-0.998930</td>\n",
       "      <td>-0.445372</td>\n",
       "      <td>-0.690700</td>\n",
       "      <td>-1.394245</td>\n",
       "      <td>0.299983</td>\n",
       "      <td>-0.502676</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.740472</td>\n",
       "      <td>-0.021457</td>\n",
       "      <td>0.776463</td>\n",
       "      <td>0.140992</td>\n",
       "      <td>-0.044228</td>\n",
       "      <td>0.506876</td>\n",
       "      <td>-0.745966</td>\n",
       "      <td>-0.446429</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.139933</td>\n",
       "      <td>0.051123</td>\n",
       "      <td>-2.041679</td>\n",
       "      <td>0.146750</td>\n",
       "      <td>0.728256</td>\n",
       "      <td>0.546757</td>\n",
       "      <td>4.568139</td>\n",
       "      <td>-0.363719</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies_tfidf  Glucose_tfidf  BloodPressure_tfidf  SkinThickness_tfidf  \\\n",
       "0           0.521271       0.800996            -0.367192             0.308084   \n",
       "1          -0.789470      -0.895213             0.201283             0.465320   \n",
       "2           0.907893       1.690672            -0.998930            -0.445372   \n",
       "3          -0.740472      -0.021457             0.776463             0.140992   \n",
       "4          -1.139933       0.051123            -2.041679             0.146750   \n",
       "\n",
       "   Insulin_tfidf  BMI_tfidf  DiabetesPedigreeFunction_tfidf  Age_tfidf  \\\n",
       "0      -0.386666  -0.187761                        0.315661   1.107942   \n",
       "1       0.684506  -0.180681                       -0.186748   0.259045   \n",
       "2      -0.690700  -1.394245                        0.299983  -0.502676   \n",
       "3      -0.044228   0.506876                       -0.745966  -0.446429   \n",
       "4       0.728256   0.546757                        4.568139  -0.363719   \n",
       "\n",
       "   Outcome  \n",
       "0        1  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存为csv格式\n",
    "X_train_tfidf = pd.DataFrame(columns = feat_names_tfidf, data = X_train_tfidf)\n",
    "train_tfidf = pd.concat([X_train_tfidf, y_train], axis = 1)\n",
    "train_tfidf.to_csv('FE_pima-indians-diabetes_tfidf.csv',index = False,header=True)\n",
    "train_tfidf.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
